package com.example.demo;

//import org.springframework.util.StringUtils;

import com.example.demo.utils.CheckPwdUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;

public class CheckPasswdTest {

    public static final String FILE_NAME="weak_pass.txt";

    public static final String OUT_FILE_PATH="E:\\git_work\\weak_password\\filtered_weak_pass.txt";

    public static final String[] WEAK_PASSWORD_ARRAY={
            "123456",
            "Q1W2E3R4",
            "q1w2e3r4",
            "1QAZ2WSX",
            "1qaz2wsx",
            "123456QWERTY",
            "123456qwerty",
            "11111111",
            "123qwe!@#",
            "123qweasd",
            "12681268",
            "192.168.0.1",
            "192.168.1.1",
            "192.168.2.1",
            "777!@#$%",
            "7ujm8ik,",
            "a1b2c3d4",
            "admin1234",
            "admin4488",
            "admin66666"
    };

    public static void main(String[] args) {
        CheckPasswdTest checkPasswdTest =new CheckPasswdTest();
        String passwd=null;
        System.out.println("=================== program start !===================");
        long start=System.currentTimeMillis();
        try {
            passwd= checkPasswdTest.getStringFromResourceFile(FILE_NAME);
        } catch (IOException e) {
            e.printStackTrace();
        }
        long loadEnd=System.currentTimeMillis();
        System.out.println("=================== load file end cost "+(loadEnd-start)+" ms!===================");

//        for (int i = 0; i < WEAK_PASSWORD_ARRAY.length; i++) {
//            if ((WEAK_PASSWORD_ARRAY[i]!=null || "".equals(WEAK_PASSWORD_ARRAY[i]))&&passwd.indexOf(WEAK_PASSWORD_ARRAY[i]) >=0) {
//                System.out.println(WEAK_PASSWORD_ARRAY[i]+" is in weak password!");
//            }
//        }

        try {
            saveString2File(OUT_FILE_PATH,passwd);
        } catch (IOException e) {
            e.printStackTrace();
        }
        long checkEnd=System.currentTimeMillis();
        System.out.println("=================== write file end cost "+(checkEnd-loadEnd)+" ms!===================");
    }

    public String getStringFromResourceFile(String fileName) throws IOException {
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder=new StringBuilder();
        String line=null;
        while ((line = bufferedReader.readLine()) != null) {
            // 拼接换行符
            if (CheckPwdUtils.EvalPWD(line)) {
                stringBuilder.append(line + "\n");
                System.out.println(line);
            }
        }
        inputStream.close();
        bufferedReader.close();
        return stringBuilder.toString();
    }

    public static void saveString2File(String path,String content) throws IOException {
        Files.write(Paths.get(path), content.getBytes());
    }
}
